<?php 
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=07c","root","root");
$redis = new redis();
$redis->connect("127.0.0.1","6379");

// print_r($data);
//当前页
$page = isset($_GET['page'])?$_GET['page']:1;
//每页的条数
$num = 3;
//总条数
$sqls = "select count(*) from shop";
$res = $pdo->query($sqls)->fetch();
// print_r($res);
$pagenum = $res[0];
// print_r($totelpage);
//总页数
$totelpage = ceil($pagenum/$num);
//偏移量
$limit = ($page-1)*$num;
if($redis->get('datas'.$page))
{
	echo "这是redis数据";
	$datas = $redis->get('datas'.$page);
	$data = json_decode($datas,true);
}
else
{
	$sql = "select * from shop limit $limit,$num";
	$data = $pdo->query($sql)->fetchall(PDO::FETCH_ASSOC);
	$datas = json_encode($data);
	$redis->set("datas".$page,$datas);
}

//上一页
$last = $page-1>0?$page-1:1;
//下一页
$next = $page+1<$totelpage?$page+1:$totelpage;
$str = "<a href='page.php?page=1'>首页</a><a href='page.php?page=$last'>上一页</a><a href='page.php?page=$next'>下一页</a><a href='page.php?page=$totelpage'>尾页</a>";


 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
 	<title>Document</title>
 </head>
 <body>
 	<center>
 		<table border="1">
 			<tr>
 				<td>商品</td>
 				<td>商品分类</td>
 				<td>商品品牌</td>
 				<td>惊爆价</td>
 				<td>库存量</td>
 				<td>操作</td>
 			</tr>
 			<?php foreach ($data as $key => $value): ?>
 				<tr>
	 				<td><?php echo $value['shop_name']; ?></td>
	 				<td><?php echo $value['shop_type']; ?></td>
	 				<td><?php echo $value['shop_pin']; ?></td>
	 				<td><?php echo $value['shop_price']; ?></td>
	 				<td><?php echo $value['shop_num']; ?></td>
	 				<td><a href="desc.php?id=<?php echo $value['shop_id']; ?>">商品详情</a></td>
	 			</tr>
 			<?php endforeach ?>
 		</table>
 		<?php echo $str ?>
 	</center>
 </body>
 </html>